program rad2brt

    use iasing_osse, only:  &
        JPI4,               &
        JPR4,               &
        JPR8,               &
        NIASI,              &
        NIANG,              &
        r4a_ids,            &
        r60_dat
        
    use netcdf

    implicit none
    !
    integer             :: ja
    ! 
    character(len=128)  :: dbfile
    character(len=3)    :: units
    integer             :: dbid
    type(r4a_ids)       :: db_ids
    type(r60_dat)       :: db_dat
    ! 
    logical             :: lex, torad, tobrt
    logical             :: brt_units = .false.
    integer(kind=JPI4)  :: natm
    real(JPR4), allocatable, dimension(:,:) :: ria, rng, ngb

    real(JPR4)          :: wia(NIASI), wng(NIANG)

    include "checknc.interface"
    include "rad2brt_f.interface"
    include "brt2rad_f.interface"
    include "r60_initializer.interface"

    ! Get argument 
    call get_command_argument(1, dbfile)
    if (len_trim(dbfile)==0) then
        print*, "No file provided. Aborting..."
        stop -1
    else
        inquire(file=dbfile, exist=lex)
        if (.not.lex) then
            write(*,'(3A)') "File ", trim(dbfile), " does not exits. STOP"
            stop -2
        endif
    endif
    print*, "dbfile: ", trim(dbfile)

    ! Get desired units
    call get_command_argument(2, units)
    if (units == "rad") then
        torad = .true.
    else if (units == "brt") then
        tobrt = .true.
    else
        print*, "Bad unit specification. Options: rad/brt"
        stop -3
    endif
    print*, "Desired units: ", units

    ! open and initialize databank 
    print*, "previous openning file"
    call checknc(nf90_open(trim(dbfile), NF90_WRITE, dbid))
    db_ids = r60_initializer(dbid)
    print*, "File opened and initialized..."

    call checknc(nf90_inquire_dimension(dbid, db_ids%atm_did, len=natm))
    print*, "Number of atmospheres ", natm

    call checknc(nf90_get_var(db_ids%dim%gid, db_ids%dim%wia, wia))
    call checknc(nf90_get_var(db_ids%dim%gid, db_ids%dim%wng, wng))
    print*, "Wavenumbers variables loaded..."

    ! Allocation of vars
    allocate(ria(NIASI, natm))
    allocate(rng(NIANG, natm))
    allocate(ngb(NIANG, natm))
    print*, "Allocation of 'ria', rng and ngb"

    ! Load vars
    call checknc(nf90_get_var(db_ids%rbt%gid, db_ids%rbt%ria, ria))
    call checknc(nf90_get_var(db_ids%rbt%gid, db_ids%rbt%rng, rng))
    call checknc(nf90_get_var(db_ids%rbt%gid, db_ids%rbt%ngb, ngb))
    
    !Check units
    if (ria(1,1).lt.200) then
        brt_units = .false.
    else
        brt_units = .true.
    endif
    print*, "rng(1,1)", rng(1,1)
    print*, "rng(NIANG,1)", rng(NIANG,1)
    print*, "ngb(1,1)", ngb(1,1)
    print*, "ngb(NIANG,1)", ngb(NIANG,1)

    if ( (tobrt .and. brt_units) .or. &
         (torad .and. .not. brt_units) ) then
        print*, "No need of changing units"
        stop 0
    else if (tobrt) then
        do ja = 1, natm
            ria(:,ja) = rad2brt_f(NIASI, wia, ria(:,ja))
            rng(:,ja) = rad2brt_f(NIANG, wng, rng(:,ja))
            ngb(:,ja) = rad2brt_f(NIANG, wng, ngb(:,ja))
        enddo
    else if (torad) then
        do ja = 1, natm
            ria(:,ja) = brt2rad_f(NIASI, wia, ria(:,ja))
            rng(:,ja) = brt2rad_f(NIANG, wng, rng(:,ja))
            ngb(:,ja) = brt2rad_f(NIANG, wng, ngb(:,ja))
        enddo
    else
        print*, "Error in if change units???"
        stop -4
    endif 

    print*, "rng(1,1)", rng(1,1)
    print*, "rng(NIANG,1)", rng(NIANG,1)
    print*, "ngb(1,1)", ngb(1,1)
    print*, "ngb(NIANG,1)", ngb(NIANG,1)

    print*, "Begin of writting process"
    call checknc(nf90_put_var(db_ids%rbt%gid, db_ids%rbt%ria, ria))
    print*, "ria OK"
    call checknc(nf90_put_var(db_ids%rbt%gid, db_ids%rbt%rng, rng))
    print*, "rng OK"
    call checknc(nf90_put_var(db_ids%rbt%gid, db_ids%rbt%ngb, ngb))
    print*, "ngb OK"

    ! Deallocation
    deallocate(ria)
    deallocate(rng)
    deallocate(ngb)

    ! Close file
    call checknc(nf90_close(dbid))

end program
